/**
 * Copyright 中国电信甘肃万维公司 All rights reserved.
 * 中国电信甘肃万维公司 专有/保密源代码,未经许可禁止任何人通过任何* 渠道使用、修改源代码.
 */
package com.gsww.vue.controller.sys;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.gsww.vue.entity.sys.SysAccount;
import com.gsww.vue.entity.sys.SysLog;
import com.gsww.vue.entity.sys.SysUserSession;
import com.gsww.vue.service.sys.SysLoginService;
import com.gsww.vue.service.sys.SysMenuService;
import com.gsww.vue.utils.Md5;
import com.gsww.vue.utils.TimeHelper;

/**
 * <p>Copyright: Copyright (c) 2011</p>
 * <p>公司名称 : 中国电信甘肃万维公司</p>
 * <p>项目名称 : jup-web</p>
 * <p>创建时间 : 2014年7月23日 下午10:57:16</p>
 * <p>类描述 : 用户登录controller        </p>
 *
 *
 * @version 1.0.0
 * @author <a href=" ">lzxij</a>
 */
@RestController 
public class SysLoginController {
	@Autowired
	private SysLoginService sysLoginService;
	@Autowired
	private SysMenuService sysMenuService;
	
	
	/**
	 * 验证用户账号及密码
	 * @param request
	 * @param response
	 * @return mav
	 * @throws IOException 
	 */
	@RequestMapping(value = "/login/sysLogin", method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> login(@RequestBody Map<String,Object> params,HttpServletRequest request, HttpServletResponse response) throws IOException {
		String userName = params.get("userName").toString();
		String passWord = params.get("password").toString();
		//String kaptcha = params.get("kaptcha").toString();
		String sessionKaptcha = (String) request.getSession().getAttribute("kaptcha");
		System.out.println(sessionKaptcha);
		Map<String, Object> resMap = new HashMap<String, Object>();
		//String loginIp = getIpAddr(request);
		//为了自动化测试先屏蔽掉验证码，请大家先别修改回去，手下留情。谢谢。
		if (true) {
//		if (sessionKaptcha.equals(kaptcha)) {
			// 验证码正确，检查用户名和密码
			try {
				SysAccount user = sysLoginService.login(userName, new Md5().getMD5ofStr(passWord));
				//SysUserSession sysUserSession = sysLoginService.login(userName, new Md5().getMD5ofStr(passWord));
				if (user != null) {
					//JSONArray menuObj = JSONArray.fromObject(sysMenuService.getAllMenu());
					//sysUserSession.setMenuList(menuObj.toString());
					if(user.getUserState().equals("1")){
						//request.getSession().setAttribute("sysUserSession", sysUserSession);
						resMap.put("ret", "0");
						resMap.put("msg", "登录成功！");
						resMap.put("user", user);
						resMap.put("token", "aaabbb");
						//response.getWriter().write(JSONObject.toJSONString(resMap));
						try {
							// 登录日志
							SysLog log = new SysLog();
							log.setUserAcctId(user.getUserAcctId());
							//log.setLogIp(user.get);
							log.setLogModel("系统登录");
							log.setLogModelOperator(userName);
							log.setLogType("1");
							log.setLogContent("成功");
							log.setOperatorTime(TimeHelper.getCurrentTime());
							//sysLogService.logInsert(log);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}else{
						resMap.put("ret", "3");
						resMap.put("msg", "用户已停用！");
						//response.getWriter().write(JSONObject.toJSONString(resMap));
						try {
							// 登录日志
							SysLog log = new SysLog();
							log.setUserAcctId(user.getUserAcctId());
							log.setLogModel("系统登录");
							log.setLogModelOperator(userName);
							log.setLogType("1");
							log.setLogContent("失败");
							log.setOperatorTime(TimeHelper.getCurrentTime());
							//sysLogService.logInsert(log);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				} else {
					resMap.put("ret", "2");
					resMap.put("msg", "用户名或密码错误！");
					//response.getWriter().write(JSONObject.toJSONString(resMap));
					try {
						// 登录日志
						SysLog log = new SysLog();
						log.setUserAcctId("-999");
						//log.setLogIp(loginIp);
						log.setLogModel("系统登录");
						log.setLogModelOperator(userName);
						log.setLogType("1");
						log.setLogContent("失败");
						log.setOperatorTime(TimeHelper.getCurrentTime());
//						sysLogService.logInsert(log);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				resMap.put("ret", "4");
				resMap.put("msg", "系统错误！");
				//response.getWriter().write(JSONObject.toJSONString(resMap));
			}
		} 
		else {
			// 验证码错误，返回首页并提示验证码错误
			resMap.put("ret", "1");
			resMap.put("msg", "验证码错误！");
		}
		return resMap;
		//return JsonHelper.jsonSuccess(resMap);
	}
	/**
	 * 获取用户信息
	 * @param request
	 * @param response
	 * @return mav
	 * @throws IOException 
	 */
	@RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
	public Map<String,Object> getInfo(String userId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			SysUserSession userSession = sysLoginService.getUserInfo(userId);
			resMap.put("userInfo", userSession);
		} catch(Exception e){
			e.printStackTrace();
		}
		
		return resMap;
	}
	@RequestMapping(value = "/message/count", method = RequestMethod.GET)
	public Map<String,Object> getCount(String userId,HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			//SysUserSession userSession = sysLoginService.getUserInfo(userId);
			
			resMap.put("count", "3");
		} catch(Exception e){
			e.printStackTrace();
		}
		
		return resMap;
	}

	
}
